using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using Pampa.Common.Pages;
using Pampa.WebControls;

namespace Pampa.CRUD.Configuration
{
    public class Link
    {
        ClientButton button = new ClientButton();

        private string text;
        private string action;
        private string iconPath;
        private string iconPathOn;

        private bool enabled;
        private string permissionRequired;

        #region Properties

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        public string Action
        {
            get { return action; }
            set { action = value; }
        }

        public string IconPath
        {
            get { return iconPath; }
            set { iconPath = value; }
        }

        public string IconPathOn
        {
            get { return iconPathOn; }
            set { iconPathOn = value; }
        }

        public bool Enabled
        {
            get { return enabled; }
            set { enabled = value; }
        }

        public string PermissionRequired
        {
            get { return permissionRequired; }
            set { permissionRequired = value; }
        }

        public virtual Control Control
        {
            get
            {
                button.Text = this.Text;
                button.MethodName = this.Action;
                button.IconPath = this.IconPath;
                button.IconPathOn = this.IconPathOn; 
                button.Enabled = this.Enabled;
                button.CssClass = string.IsNullOrEmpty(this.IconPath) ? "button" : "iconButton";
                button.Type = string.IsNullOrEmpty(this.IconPath) ? Pampa.WebControls.ButtonType.Button : Pampa.WebControls.ButtonType.Image;
                button.RollOver = true;

                return button;
            }
        }

        #endregion

        #region Xml

        public static Link LoadFromXml(XmlNode node)
        {
            Assembly assembly = Assembly.GetAssembly(typeof(Link));
            object objLink = assembly.CreateInstance("Pampa.CRUD.Configuration." + node.Name);

            if (objLink is Link)
            {
                Link link = (Link)objLink;
                link.LoadXml(node);
                return link;
            }
            else
            {
                throw (new Pampa.CRUD.Exceptions.ConfigurationException("There is no Link of Type " + node.Name));
            }
        }

        public virtual void LoadXml(XmlNode node)
        {
            try
            {
                this.Text = node.Attributes["Text"].Value;
                this.Action = node.Attributes["Action"].Value;

                if (node.Attributes["IconPath"] != null)
                    this.IconPath = node.Attributes["IconPath"].Value;

                if (node.Attributes["IconPathOn"] != null)
                    this.IconPathOn = node.Attributes["IconPathOn"].Value;

                if (node.Attributes["PermissionRequired"] != null)
                    this.PermissionRequired = node.Attributes["PermissionRequired"].Value;
            }
            catch (Exception ex)
            {
                throw new Pampa.CRUD.Exceptions.ConfigurationException("Link mal definido.", ex);
            }
        }

        #endregion
    }
}
